package com.wsy.controller;


import com.wsy.common.Res;
import com.wsy.entity.Customer;
import com.wsy.entity.Line;
import com.wsy.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *wsy
 */
@RestController
@RequestMapping("/api/customer")
public class CustomerController {

    @Autowired
    CustomerService customerService;

//客户端用户
    @PostMapping("/login")
    public Res login(@RequestBody Customer customer){
        System.out.println(1);
        Customer loginCustomer = customerService.login(customer);
        if (loginCustomer != null){
            return new Res(1,"登录成功",loginCustomer);
        }else{
            return new Res(0,"登录失败");
        }
    }
//    管理员
@PostMapping("/admin")
public Res adminLogin(@RequestBody Customer customer){
    Customer loginCustomer = customerService.adminLogin(customer);
    if (loginCustomer != null){
        return new Res(1,"管理员登录成功",loginCustomer);
    }else{
        return new Res(0,"登录失败");
    }
}

    @PostMapping("/register")
    public Res register(@RequestBody Customer customer){
        Customer registerCustomer = customerService.register(customer);
        if (registerCustomer != null){
            return new Res(1,"注册成功",registerCustomer);
        }else{
            return new Res(0,"注册失败");
        }

    }

    @PostMapping("/cat")
    public List<Line> getCat(@RequestBody Line line){
        return customerService.getCat(line.getCustomerID());
    }

    @PostMapping("/get/order")
    public List<Line> getOrder(@RequestBody Line line){
        return customerService.getOrder(line.getCustomerID());
    }

//    添加订单
    @PostMapping("/order")
    public Res postOrder(@RequestBody Line line){
        if (customerService.postOrder(line) != 0){
            return new Res(1,"下单成功");
        }else {
            return new Res(0,"下单失败");
        }
    }

//    删除购物车
    @DeleteMapping("/cat/{id}")
    public Res delCat(@PathVariable("id") int id){
        if (customerService.delCat(id) != 0){
            return new Res(1,"删除成功");
        }else {
            return new Res(0,"删除失败");
        }
    }

    @GetMapping("/token")
    public Res getMyInfo(@RequestHeader("TOKEN") String token) {
        Line currentUser = customerService.getToken(token);
        currentUser.setPassword(null);
        return new Res(1, "成功", currentUser);
    }

    @GetMapping("/num")
    public List getNum() {
        return customerService.getNum();
    }
    @GetMapping("/jie")
    public Line getjie() {
        return customerService.getjie();
    }

    @PutMapping("/num/{num}")
    public void putNum(@PathVariable("num") int num){
        int item =customerService.putNum(num);
    }

    @PostMapping("/addCar/{token}")
    public void postAddcar(@PathVariable("token") String token){
        int item = customerService.postAddcar(token);
    }

}
